<?php
/*************************************************************************
Class Pick_Core_User
----------------------------------------------------------------
PICKCOCMS Project
Company: PICKCO  Co., Ltd  
Email: admin@pick.vn
Author: Hai Nam
Last updated: 30/09/2009
**************************************************************************/
/* Edit log:
- 30/09/2009 08:00 - Hai Nam: Initialize
*/
class Pick_Core_User extends Pick_Core_Objects {
	function Pick_Core_User($database = '') {
		if(!$database) {
			global $db;
			$this->_db = $db;
		} else $this->_db = $database;
		$this->typeId 		= " AND typeid = 6";
		$this->objectInfo 	= 'Pick_Core_UserInfo';
		$this->table = DB_PREFIX."user";	
	}
	function getUserInfo($userId = 0) {
		$result = $this->getObjects(1,"`id` = '$userId'");
		if(is_array($result))
			return $result['data'][0];
		return;
	}
	function getUserFromTypeId($page = 1, $userId, $sort = array(), $items_per_page = DEFAULT_ROWS_PER_PAGE) {
		$result = $this->getObjects($page,"`type` = '$userId' AND status=1", array("id"=>"DESC"), $items_per_page);
		if(is_array($result))
			return $result['data'];
		return;
	}	
	function getUserType($userId = 0) {
		$result = $this->getObjects(1,"`id` = '$userId'");
		if(is_array($result))
			return $result['data'][0]->getField('type');
		return;
	}	
	function getObjectFromUser($username = 0) {
		$result = $this->getObjects(1,"`username` = '$username'");
		if(is_array($result)){
			return $result['data'][0];
		}#endif
		return;
	}
    function getInfoCurrentUser(){
        if(!empty($_SESSION['userId'])){
            $userid = $_SESSION['userId'];
            $user = new Pick_Core_User();
            return $user->getObject($userid);
        }
        return;
    }
    function updatePassword($password){
        if(!empty($_SESSION['userId'])){
            $userid = $_SESSION['userId'];
            $user = new Pick_Core_User();
            return $user->_db->query('UPDATE  `teraco_db`.`n_user` SET  `password` =  MD5("'.$password.'") WHERE  `n_user`.`id` = "'.$userid.'"');
        }
        return;
    }
    function getObjectFromEmail($email = "") {
		$result = $this->getObjects(1,"`email` = '$email'");
		if(is_array($result)){
			return $result['data'][0];
		}#endif
		return;
	}	
	function authenticateUser($username,$password) {
		$username = str_replace(" ",'',$username);
		$username = str_replace("\\",'',$username);
		$username = str_replace("\"",'',$username);
		$username = str_replace("'",'',$username);	
		$password = md5($password);
		$result = $this->select('*',"`username` = '$username' AND `password` = '$password'");
		if($result) {
			$this->update(array('last_login'=>date("Y-m-d H:i:s")), "id='".$result[0]['id']."'");
			return $result[0]['id'];
		}
		return 0;
	}
/*-----------------------------------------------------------------------*
* Function: updatePosition
* Parameter: Info object
* Return: 1 if key already exists, 0 if not exists
*-----------------------------------------------------------------------*/	
	function changePassword($oId = 0, $password = '') {
		if(!$oId) return 0;
		if($this->update(array('password' => $password), "`id` = '$oId'")) return 1;
		return 0;
	}
	
	function duplicateEmail($email,$userId=0) {
		
		$result = $this->select('id',"email=$email and ($userId?id<>'$userId':'')");
		if($result) {
			mysql_free_result($result);
			return 1;
		}
		return 0;
	}		
	
}
?>